const posting = require("../models/posting")
const like = require("../models/like")
const replyPosting = require("../models/replyPosting")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            posting.find({
                point_id: fields.point_id
            }, null, { sort: {time: -1} }, async (err, docs) => {
                if (err) {
                    res.json({ 'state': false, 'msg': err });
                } else {
                    let user_like_list = await like.find({
                        UUID: fields.UUID
                    })
                    // let return_list = [];
                    // console.log(docs)
                    let return_list = [];
                    for(i=0; i<docs.length; i++){
                        let reply_list = await replyPosting.find({
                            replyToId: docs[i]._id
                        },null,{ sort: { time: -1 }})
                        let reply = reply_list.map(item => {
                            return{
                                authorUUID: item.authorUUID,
                                authorUsername: item.authorUsername,
                                content: item.content,
                                likeCount: item.likeCount,
                                postingId: item.postingId,
                                replyToId: item.replyToId,
                                replyToName: item.replyToName,
                                replyToUUID: item.replyToUUID,
                                time: item.time,
                                avatar: item.avatar || "头像3.svg",
                                _id: item._id,
                                likeFlag: user_like_list.length === 0 ? false : user_like_list[0].reply_list.includes(item._id)
                            }
                        })
                        return_list.push({
                            reply: reply,
                            _id: docs[i]._id,
                            menu: docs[i].menu,
                            point_title: docs[i].point_title,
                            point_id: docs[i].point_id,
                            content: docs[i].content,
                            authorUUID: docs[i].authorUUID,
                            authorUsername: docs[i].authorUsername,
                            authorRole: docs[i].authorRole,
                            likeCount: docs[i].likeCount,
                            avatar: docs[i].avatar || "头像3.svg",
                            time: docs[i].time,
                            likeFlag: user_like_list.length === 0 ? false : user_like_list[0].posting_list.includes(docs[i].id)
                        })
                    }
                        
                    
                    res.json({ 'state': true, 'result': return_list });
                }
            })
        }

    });

});

module.exports = router;